<?php

namespace api\controller;

use common\controller\VerifyController;
use common\service\TaskUpstreamTransService;
use think\Request;

class TaskUpstreamTransController extends VerifyController
{

    private $tagUpstreamTransService;

    public function __construct()
    {
        parent::__construct();
        $this->tagUpstreamTransService = TaskUpstreamTransService::getInstance();
    }


    /**
     * 保存任务的上游（或下游）任务传输状态
     * @param Request $request
     * @return \think\Response
     */
    public function saveTaskTrans(Request $request)
    {
        $param = $request->param();
        $res = $this->tagUpstreamTransService->saveTaskTrans($param['data'] ?? []);
        return json(success_response('OK', $res));
    }

    /**
     * 获取任务的上游（或下游）任务传输状态
     * @param Request $request
     * @return \think\Response
     */
    public function getTransByTaskId(Request $request)
    {
        $param = $request->param();
        $taskIds = split_array($param['param']['task_ids'] ?? []);
        $direction = $param['param']['direction'] ?? 'upstream';
        $type = $param['param']['type'] ?? 'normal';
        $res = $this->tagUpstreamTransService->getTransByTaskIdList($taskIds, $direction, $type);
        return json(success_response('OK', $res));
    }

    /**
     * 获取订单任务的上传/下载列表传输状态
     * @param Request $request
     * @return \think\Response
     */
    public function getTransByOrderId(Request $request)
    {
        $param = $request->param();
        $orderIds = split_array($param['param']['order_ids'] ?? []);
        $transType = $param['param']['direction'] ?? 'download';
        $res = $this->tagUpstreamTransService->getTransByOrderId($orderIds, $transType);
        return json(success_response('OK', $res));
    }

    /**
     * 我的订单任务，上游传输状态查询
     * @param Request $request
     * @return \think\Response
     */
    public function getMyOrderTaskTrans(Request $request)
    {
        $param = $request->param()['param'] ?? [];
        $projectIds = $param['project_ids'] ?? [];
        $episodeCodes = $param['episode_codes'] ?? [];
        $taskName = $param['task_name'] ?? "";
        $page = $param['page'] ?? [1, C('default_page_total')];
        $res = $this->tagUpstreamTransService->getMyOrderTaskTrans($page, $projectIds, $episodeCodes, $taskName);
        return json(success_response('OK', $res));
    }
}
